package com.pet_service.controller;

import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.pet_service.entity.User;
import com.pet_service.result.Result;
import com.pet_service.service.UserService;
import com.pet_service.util.JwtUtil;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 邹全伸
 * @version 1.0
 * @date 2025-09-18 14:59
 */
@Slf4j
@Tag(name = "测试接口")
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private UserService UserService;

    @GetMapping("/hello")
    public Result hello() {
        User tUser = UserService.getById(1);
        return Result.ok(tUser);
    }

    @PostMapping("/login")
    public Result test() {
        log.info("收到登录请求");
        // 生成Token
        Map<String, String> claims = new HashMap<>();
        claims.put("userId", "1");
        claims.put("username", "cust");
        String token = JwtUtil.generateToken("mySecret","pet_service","userAuth",claims);
        log.info("生成的Token：{}",token);
        log.info("准备返回结果");
        return Result.ok(token);
    }

    @GetMapping("/list")
    public Result list(@RequestHeader(required = false)String token) {
        if (token != null) {
            // 验证Token
            try {
                DecodedJWT jwt = JwtUtil.verifyToken(token,"mySecret","pet_service");
                return Result.ok("Token验证成功");
            } catch (JWTVerificationException e) {
                e.printStackTrace();
                return Result.fail(500,"Token验证失败");
            }
        }
        return Result.fail(500,"未提供Token");
    }
}
